﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using UploadClient.View;
using Microsoft.Windows.Controls;

namespace UploadClient
{
    public partial class oldSettingsWindow : Window
    {
        /// <summary>
        /// Binding to the View: IsFavourite
        /// </summary>
        readonly Favourite favourite = new Favourite(false);

        private readonly ConnectionSettings settings;
        public bool isTriggertByButton = false;

        public oldSettingsWindow(ConnectionSettings settings)
        {
            InitializeComponent();
            this.settings = settings;
            this.DataContext = favourite;

            //set default values in gui
            if (settings.Count() > 0)
            {
                if (settings.FavouriteConnection != null)
                {
                    SetControlValues(settings.FavouriteConnection);
                }
                else
                    SetControlValues(settings.First());

                OkButton.IsEnabled = true;
            }
            else
            {
                SetControlValues(new ServerSettings());
                EnableButtons();
            }

            HostadressField.Focus();
        }

        /// <summary>
        /// Closes the Window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            ServerSettings connection = settings.Find(c => c.Equals(GetControlValues()));

            if (connection == null)
                connection = GetControlValues();

            settings.Add(connection);
            settings.SelectedConnection = connection;

            if (favourite.IsFavourite)
                settings.FavouriteConnection = connection;

            isTriggertByButton = true;
            this.Close();
        }

        /// <summary>
        /// Select the connection settings.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonSpinner_Spin(object sender, SpinEventArgs e)
        {
            ButtonSpinner spinner = (ButtonSpinner)sender;
            WatermarkTextBox txtBox = (WatermarkTextBox)spinner.Content;
            int actualIndex = settings.FindIndex(c => c.Equals(GetControlValues()));
            int maxIndex = settings.Count() - 1;

            //increment
            if (e.Direction == SpinDirection.Increase)
            {
                if (actualIndex < maxIndex)
                {
                    SetControlValues(settings[actualIndex + 1]);
                    OkButton.IsEnabled = true;
                }
            }
            else //decrement
            {
                if (actualIndex > 0)
                {
                    SetControlValues(settings[actualIndex - 1]);
                    OkButton.IsEnabled = true;
                }
                else
                {
                    SetControlValues(new ServerSettings());
                    EnableButtons();
                }
            }
        }

        /// <summary>
        /// Select a connection as favourite.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StarButton_Click(object sender, RoutedEventArgs e)
        {
            var result = settings.Find(c => c.Equals(GetControlValues()));
            if (result != null)
            {
                if (settings.FavouriteConnection != null && settings.FavouriteConnection.Equals(result))
                {
                    settings.FavouriteConnection = null;
                    favourite.IsFavourite = false;
                }
                else
                {
                    settings.FavouriteConnection = result;
                    favourite.IsFavourite = true;
                }
            }
            else favourite.IsFavourite = !favourite.IsFavourite;
        }

        /// <summary>
        /// Deletes a connection.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            var result = settings.Find(c => c.Equals(GetControlValues()));
            if (result != null)
                settings.Remove(result);

            SetControlValues(new ServerSettings());

        }

        /// <summary>
        /// Updates the controls with the settings value.
        /// </summary>
        /// <param name="csettings">Settings to update the gui with.</param>
        private void SetControlValues(ServerSettings csettings)
        {
            HostadressField.Text = csettings.Host;
            UsernameField.Text = csettings.Username;
            PasswordField.Password = csettings.Password;
            PassivCheckBox.IsChecked = csettings.Passive;
            PortField.Text = csettings.Port.ToString();

            if (settings.FavouriteConnection != null && settings.FavouriteConnection.Equals(csettings))
                favourite.IsFavourite = true;
            else favourite.IsFavourite = false;
        }

        /// <summary>
        /// Return a settings object from gui values.
        /// </summary>
        /// <returns>Settings object.</returns>
        private ServerSettings GetControlValues()
        {
            //ToDo: string in port-textbox? -> trow exception/info!
            int port;
            Int32.TryParse(PortField.Text, out port);

            return new ServerSettings
            {
                Host = HostadressField.Text,
                Username = UsernameField.Text,
                Password = PasswordField.Password,
                Passive = (PassivCheckBox.IsChecked.HasValue) ? PassivCheckBox.IsChecked.Value : false,
                Port = port,
            };
        }

        private void Hostadress_TextChanged(object sender, TextChangedEventArgs e)
        {
            EnableButtons();
        }

        /// <summary>
        /// Enable buttons if HostadressField has text.
        /// Disable if not!
        /// </summary>
        private void EnableButtons()
        {
            if (!HostadressField.Text.Equals(string.Empty))
            {
                StarButton.IsEnabled = true;
                DeleteButton.IsEnabled = true;
                OkButton.IsEnabled = true;
            }
            else
            {
                StarButton.IsEnabled = false;
                DeleteButton.IsEnabled = false;
                OkButton.IsEnabled = false;
            }
        }
    }
}
